<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------

namespace app\adminapi\controller\v1\car;


use app\adminapi\controller\AuthController;
use app\services\car\CarContactServices;
use think\facade\App;

/**
 * Class CarContact
 * @package app\adminapi\controller\v1\shendeng
 */
class CarContact extends AuthController
{
    public function __construct(App $app,CarContactServices $services)
    {
        parent::__construct($app);
        $this->services = $services;
    }

    /**
     * 列表
     * @return \think\Response
     */
    public function getList(){
        $where = $this->request->getMore([
            ['keyword',''],
        ]);
        return app('json')->success($this->services->getList($where,true));
    }

    /**
     * 添加修改
     * @return mixed
     * @throws \FormBuilder\Exception\FormBuilderException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add(){
        $data = $this->request->getMore([
            ['id', 0],
        ]);
        return app('json')->success($this->services->add((int)$data['id']));
    }

    /**
     * 保存
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function save(){
        $data = $this->request->postMore([
            ['id', 0],
            ['name', ''],
            ['phone', ''],
            ['card_no', ''],
            ['card_a', ''],
            ['card_b', ''],
            ['car_cert', ''],
            ['car_cert2', ''],
        ]);
        if (!$data['name']) return app('json')->fail('请输入姓名');
        if (!$data['phone']) return app('json')->fail('请输入手机号');
        if (!check_phone($data['phone'])) return app('json')->fail('手机号格式错误');
        if (!$data['card_no']) return app('json')->fail('请输入身份证号');
        if (!check_card($data['card_no'])) return app('json')->fail('身份证号格式错误');
        if (!$data['card_a']) return app('json')->fail('请上传身份证正面图');
        if (!$data['card_b']) return app('json')->fail('请上传身份证反面图');
        if (!$data['car_cert']) return app('json')->fail('请上传驾驶证正面图');
        if (!$data['car_cert2']) return app('json')->fail('请上传驾驶证反面图');
        $this->services->save((int)$data['id'], $data);
        return app('json')->success('提交成功');
    }

    /**
     * 删除
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete(){
        $data = $this->request->getMore([
            ['id', 0],
        ]);
        if (!$data['id']) return app('json')->fail('参数错误');
        return app('json')->success($this->services->delUser((int)$data['id']));
    }

    /**
     * 资质审核
     * @return mixed
     */
    public function auditSave(){
        $data = $this->request->postMore([
            ['id',''],
            ['status',''],
        ]);
        if(!$data['id']) return app('json')->fail('参数错误');
        return app('json')->success($this->services->auditSave((int)$data['id'],$data));
    }
}
